let isDrawingCard = false;
let account = 50;
const hintText1 = new TextRenderer(140, 350, 'black', '单击卡面 开始抽卡', 30, 'center');
const hintText2 = new TextRenderer(280, 140, 'blue', `ACCOUNT: ${account} RMB`, 50);
const hintText3 = new TextRenderer(280, 280, 'red', '每次抽卡10元哦', 35);

let myItem = null;

function getRandom(min = 1, max = 15) {
    return Math.floor((Math.random() * (max - min + 1)) + min);
}

core.onStart = function () {

    const gameContainer = new DisplayObjectContainer(0, 0);

    class Item {

        imageIndex = 1;
        rarity = null;
        prize = 0;

        itemContainer = null;
        cardImage = null;
        backgroundRect = null;
        rarityText = null;

        rarityWithColor = {
            ['R']: 'blue',
            ['SR']: 'purple',
            ['SSR']: 'gold',
            ['SSSR']: 'red'
        }

        judgeRarityByimageIndex(imageIndex) {
            if (imageIndex > 0 && imageIndex <= 8) {
                return 'R';
            } else if (imageIndex <= 12) {
                return 'SR'
            } else if (imageIndex <= 14) {
                return 'SSR'
            } else {
                return 'SSSR'
            }
        }

        constructor(imageIndex) {
            const rarity = this.judgeRarityByimageIndex(imageIndex);
            this.rarity = rarity;
            this.imageIndex = imageIndex;

            const itemContainer = new DisplayObjectContainer(100, 100);
            const cardImage = new ImageRenderer(0, 0, `img/item${imageIndex}.png`);
            const backgroundRect = new RectRenderer(0, 1024, 883, 300, this.rarityWithColor[rarity]);
            const rarityText = new TextRenderer(225, 620, 'white', rarity, 200, 'center');

            itemContainer.addChild(cardImage);
            itemContainer.addChild(backgroundRect);
            itemContainer.addChild(rarityText);

            itemContainer.scaleX = 0.4;
            itemContainer.scaleY = 0.4;

            this.itemContainer = itemContainer;
            this.cardImage = cardImage;
            this.rarityText = rarityText;
            this.backgroundRect = backgroundRect;

            gameContainer.addChild(itemContainer);
        }

        updateImage(imageIndex) {
            this.imageIndex = imageIndex;
            this.rarity = this.judgeRarityByimageIndex(imageIndex);

            this.render();
        }


        render() {
            this.cardImage.image = `img/item${this.imageIndex}.png`;
            this.backgroundRect.color = this.rarityWithColor[this.rarity];
            this.rarityText.text = this.rarity;
        }

        gainPrize(){
            switch(this.rarity){
                case 'R':
                    this.prize = getRandom(1,5);
                    return this.prize;
                case 'SR':
                    this.prize = getRandom(4, 12);
                    return this.prize;
                case 'SSR':
                    this.prize = getRandom(10, 20);
                    return this.prize;
                case 'SSSR':
                    this.prize = getRandom(25, 50);
                    return this.prize;
                default:
                    alert("出现了未知错误！");
                    return null;
            }
        }

    }

    let item = new Item(getRandom());
    myItem = item;

    item.cardImage.onClick = () => {
        if(!isDrawingCard && account < 10){
            alert("你没钱了！请重开游戏");
            return;
        }

        isDrawingCard = !isDrawingCard;

        if (isDrawingCard) {
            account -= 10;
            hintText1.text = "再次单击卡面 停止抽卡";
            hintText2.text = `ACCOUNT: ${account} RMB`;
            hintText3.color = "red";
            hintText3.text = "你的账户扣除10元";
        } else {
            const winPize = item.gainPrize();
            account += winPize;
            hintText2.text = `ACCOUNT: ${account} RMB`;
            hintText3.color = "orange";
            hintText3.text = `恭喜你获得了${winPize} RMB!`;
            hintText1.text = "单击卡面开始抽卡";
        }
    }


    gameContainer.addChild(hintText1);
    gameContainer.addChild(hintText2);
    gameContainer.addChild(hintText3);
    core.root.addChild(gameContainer);
}

core.onUpdate = function () {
    if (isDrawingCard) {
        myItem.updateImage(getRandom());
    }
}